/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.formulae;

import org.eclipse.jdt.core.dom.ITypeBinding;

/**
 * {@link #equals(Object)} and {@link #hashCode()} are <b>not</b> deep
 * because {@link #getArray()} and {@link #getIndex()} can be
 * arbitrary {@link Formula}e, which may not compare properly anyhow.
 * Array access {@code a[i]}, where {@code a} and {@code i} are formulae.
 * @author kevin
 * @since Apr 24, 2010
 * @see org.eclipse.jdt.core.dom.ArrayAccess
 */
public class Select implements Name {

	private final Formula array;
	private final Formula index;
	private final ITypeBinding type;
	private final int incarnation;

	/**
	 * @param array
	 * @param index
	 * @param type array element type (aka the type of this formula)
	 */
	public Select(Formula array, Formula index, ITypeBinding type) {
		this(array, index, type, 0);
	}

	/**
	 * @param array
	 * @param index
	 * @param type array element type (aka the type of this formula)
	 */
	public Select(Formula array, Formula index, ITypeBinding type, int incarnation) {
		super();
		this.array = array;
		this.index = index;
		this.type = type;
		this.incarnation = incarnation;
	}

	public Formula getArray() {
		return array;
	}

	public Formula getIndex() {
		return index;
	}

	public ITypeBinding getType() {
		return type;
	}

	public int getIncarnation() {
		return incarnation;
	}

	@Override
	public <T> T dispatch(FormulaVisitor<T> visitor) {
		return visitor.visit(this);
	}

	@Override
	public boolean isBoolean() {
		return TypingUtil.isBoolean(type);
	}

	@Override
	public String toString() {
		if (incarnation == 0)
			return array + "[" + index + "]";
		else
			return array + "[" + index + "](" + incarnation + ")";
	}
}
